package com.soft.controller;

import com.soft.pojo.PageBean;
import com.soft.pojo.Position;
import com.soft.service.PositionService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ruoxi
 * @create 2021-12-26 20:27
 */
@WebServlet("/showAllPositionByPage")
public class ShowAllPositionByPageServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PositionService positionService = new PositionService();
        PageBean<Position> positionPageBean = new PageBean();

        int currentPage = 1;
        String currentPage_ = req.getParameter("currentPage");
        if (currentPage_ != null) {
            currentPage = Integer.parseInt(currentPage_);
        }

        int currentCount = 10;

        positionPageBean.setCurrentPage(currentPage);
        positionPageBean.setCurrentCount(currentCount);
        positionPageBean.setTotalCount(positionService.findPositionCount());
        positionPageBean.setTotalPage((int) Math.ceil(positionService.findPositionCount() * 1.0 / currentCount));
        positionPageBean.setList(positionService.findPositionByPage((currentPage - 1) * currentCount, currentCount));

        req.setAttribute("posiPg", positionPageBean);
        req.getRequestDispatcher("position.jsp").forward(req, resp);
    }
}
